{include file="public/header"/}
<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">
<link rel="stylesheet" type="text/css" href="/public/static/admin/easyui/jeasyui/themes/icon.css">
<style>
	.matchCon {
		width: 280px;
	}

	.chk-list {
		line-height: 28px;
	}

	.url-box {
		height: 28px;
		line-height: 28px;
		cursor: pointer;
	}

	.url-box a {
		margin-right: 5px;
	}

	.layui-input,
	.layui-select,
	.layui-textarea {
		height: 30px;
	}

	.ul-inline li i {
		padding: 0;
	}

	.layui-form-checked[lay-skin=primary] i {
		border-color: #4dc2e6 !important;
		background-color: #4dc2e6;
	}

	.layui-form-select dl {
		max-height: 192px !important;
	}

	.mod-choose-input {
		position: relative;
	}

	.ui-input {
		width: 180px;
	}
</style>

<div class="bill-ser-top">
	<ul class="ul-inline cf layui-form">

		<!-- 选择客户 -->
		{include file="admin@select/customer"/}

		<!-- 选择商品 -->
		{include file="admin@select/goods"/}

		<li><a class="ui-btn ui-btn-search" id="search">查询</a></li>
		<li class="url-box" id="openUrl">
			<!-- <a href="https://club.kingdee.com/club/newclub/school/course?product_id=7&amp;cid=1547#pid=5693" target="_blank" class="video-icon"></a>商品管理 -->
		</li>
	</ul>
</div>



<div class="wrapper btc">
	<div class="ui-config-box">
		<span id="config" class="ui-config">
			<a href="#" class="ui-icon-config-new"></a>
			列设置
		</span>
	</div>

	<div class="bill-ser-botm cf">
		<div class="fr ml10">
			<a href="#" class="ui-btn-bill ui-btn-add mrb" id="btn-add"
				onclick="lopen('add.html','新增', '670px', '500px')">新增</a>
			<a href="#" class="ui-btn-bill mrb" id="btn-import">导入</a>
			<a class="ui-btn-bill mrb" id="btn-export">导出</a>
			<a href="#" class="ui-btn-bill" id="btn-batchDel">删除</a>
		</div>
	</div>
	<div class="grid-wrap">
		<table id="grid">
		</table>
		<div id="page"></div>
	</div>
</div>

<script src="/public/static/admin/easyui/layui-v2.6.8/layui.js"></script>
<script type="text/javascript">

	function lopen(url, title, width, height) {
		layer.open({
			type: 2,
			title: title,
			shadeClose: true,
			shade: 0.3,
			maxmin: true, //开启最大化最小化按钮
			btn: ['确定', '取消'],
			area: [width, height],
			content: url,
			yes: function (index, layero) {
				var body = layer.getChildFrame('body', index);
				var customer_basedata = body.find('#manage-form').serialize();
				customer_basedata = unserialize(customer_basedata);

				var customerId = body.find('#customer').val();  //客户id
				var customer_extdata = body.find('#grid').jqGrid('getGridParam', 'data');
				var iframe = window['layui-layer-iframe' + index];

				var bodyGrid = body.find('#grid');


				iframe.saveTab();

				var flag = 0;
				var hasuser = 0;
				var hasuser1 = 0;
				var indexArr = [];
				// console.log(customer_extdata);
				customer_extdata.forEach(function (self, index) {
					if (self['goods_id'] != null && self['goods_id'] != "") {
						hasuser = hasuser + 1;
						if (self['number'] == null || self['number'] == "") {
							flag = 1;
							indexArr.push(index);
						} else {
							hasuser1 = hasuser1 + 1;
						}
					}
				});
				if (hasuser == 0) {
					layer.msg("请选择商品！");
					// 必填项（商品）
					iframe.chooseGood();
					return false;
				}

				if (flag == 1) {
					layer.msg("物料编码不能为空！");
					// 必填项
					iframe.chooseNumber(indexArr[0] + 1);
					return false;
				}

				if (customerId == "" || customerId == null) {
					layer.msg("请选择客户！");
				}



				// 刷新表格值
				body.find("#reloadTable").click();


				$.ajax({
					type: 'POST',
					url: "save.html",
					data: { customer_basedata: customer_basedata, customer_extdata: customer_extdata },
					dataType: "json",
					async: false,
					error: function (request) {
						alert("发送请求失败！");
					},
					success: function (data) {
						if (data.status == 1) {
							layer.msg(data.msg);
							layer.close(index);
							reload();
						} else {
							layer.msg(data.msg);
						}
					}
				});
			}
		});
	}

	//$(function(){
	$(document).ready(function () {

		// 查询按钮
		$("#search").on("click", function () {
			var customerId = $("#user_id").val();
			var goodsId = $("#goods_id").val();
			$("#grid").jqGrid('setGridParam', {
				datatype: 'json',
				postData: {
					ajax: '100',
					customer_id: customerId,
					goods_id: goodsId
				}, //发送数据 
				page: 1
			}).trigger("reloadGrid"); //重新载入 
		});

		//alert("1111");
		$("#grid").jqGrid({
			url: '/admin/customerinvnumber/index.html?ajax=100',
			datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
			height: '90%',//高度，表格高度。可为数值、百分比或'auto'
			//width: Public.setGrid().w,
			//height: Public.setGrid().h,
			colNames: ["操作", "客户", "商品", "单位", "客户物料编码"],
			colModel: [
				{ name: 'id', index: 'id', width: 70, formatter: cLink },
				{ name: 'customer_name', width: 200 },
				{ name: 'goods_name', width: 200 },
				{ name: 'unit_name', width: 100 },
				{ name: 'number', width: 120 },
			],
			rowNum: 10,
			rowList: [10, 20, 30],
			sortname: 'id',
			sortorder: "desc",
			pagerpos: "left",//分页位置
			multiselect: true,  //自带多选框
			pgbuttons: true,//翻页按钮
			pagination: true,
			altRows: !0,
			gridview: !0,
			shrinkToFit: !1,
			cellLayout: 8,
			autowidth: !0,
			pager: "#page",
			viewrecords: !0,
			cmTemplate: {
				sortable: !1,
				title: !1
			},
			loadError: function (t, e, i) {
				parent.Public.tips({
					type: 1,
					content: "操作失败了哦，请检查您的网络链接！"
				})
			}
		});

		function cbox(cellvalue, options, rowObject) {
			return '<input role="checkbox" type="checkbox" id="jqg_grid_1291309293118595_0" class="cbox" name="jqg_grid_2">'
		}

		function cLink(cellvalue, options, rowObject) {
			return '<div class="operating"><span class="ui-icon ui-icon-pencil" onclick="lopen(\'edit.html?id=' + rowObject.id + '\',\'编辑\',\'670px\',\'500px\')" title="修改"></span><span class="ui-icon ui-icon-trash" onclick="del(' + rowObject.id + ')" title="删除"></span></div>'
		}

		function cStatus(cellvalue, options, rowObject) {
			if (rowObject.status == 0) {
				return '<span class="set-status open" data-delete="false" onclick="switchs(this,' + rowObject.id + ')"><span class="ui-icon-circle"></span></span>';
			} else {
				return '<span class="set-status close" data-delete="false" onclick="switchs(this,' + rowObject.id + ')"><span class="ui-icon-circle"></span></span>';
			}

		}
	});

	function switchs(obj, id) {
		if ($(obj).hasClass("open")) {
			$(obj).removeClass("open");
			$(obj).addClass("close");
			status = 1;
		} else {
			$(obj).removeClass("close");
			$(obj).addClass("open");
			status = 0;
		}

		$.ajax({
			type: 'POST',
			url: "save.html",
			data: { id: id, status: status },
			dataType: "json",
			async: false,
			error: function (request) {
				layer.msg("发送请求失败！");
			},
			success: function (data) {
				if (data.status == 1) {
					layer.msg(data.msg);
				} else {
					layer.msg(data.msg);
				}

			}
		});
	}

</script>


{include file="public/footer"/}